﻿using AutoMapper;
using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.Domain.Model;
using HuaHan.WorkTicket.Domain.Repositories;
using HuaHan.WorkTicket.Domain.Repositories.EntityFramework;
using HuaHan.WorkTicket.ServiceContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkTicket.Application.Implementation
{
    public class CustomerServiceImpl : BaseServiceImpl<Customer>, ICustomerService
    {
        private const string NO_STRING = "CUST";
        #region Private Fields
        private UnitOfWork unitOfWork = new UnitOfWork();
        #endregion

        public CustomerServiceImpl()
        {
            base.Repository = unitOfWork.CustomerRepository;
            base.UnitOfWork = this.unitOfWork;
        }

        public string GetNextNo()
        {
            string currentYear=DateTime.Now.Year.ToString("0000");
            string currentMonth=DateTime.Now.Month.ToString("00");
            string currentDay=DateTime.Now.Day.ToString("00");
            string dateString=currentYear+currentMonth+currentDay;
            if (this.GetAll().Count() == 0)
            {
                return NO_STRING + dateString + "001";
            }
            else
            {
                string MaxNo = base.Repository.GetAll().Max(c => c.NO);
                int temp = int.Parse(MaxNo.Substring(MaxNo.Length - 3, 3));
                return NO_STRING + dateString + (temp+1).ToString("000");
            }
        }

        public bool CheckNameExisted(string name, Guid id)
        {
            var count = base.Repository.GetAll().Where(c => c.CN_Name == name && c.CustomerID != id).Count();
            if (count > 0)
                return false;
            else
                return true;
        }

    }
}
